package com.example.chat.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Admin;
import com.example.chat.beans.Report;
import com.example.chat.mapper.AdminMapper;
import com.example.chat.mapper.ReportMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import com.example.chat.result.ListResult;
import com.example.chat.result.ReportResult;
import com.example.chat.utils.CheckUtils;
import com.example.chat.utils.FileHandleUtil;
import com.example.chat.utils.Global;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ReportService extends ServiceImpl<ReportMapper, Report> {
    @Resource
    CheckUtils checkUtils;
    @Value("${global.config.path}")
    String configPath;
    @Value("${global.config.website}")
    String website;
    @Resource
    Global global;
    @Resource
    AdminMapper adminMapper;

    public ListResult<List<ReportResult>> listReport(Integer userId, String token, String tel, String phone, int reportStatus, Integer page, Integer limit) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "1201");
        if (apiResult.getStatus() != 1)
            return new ListResult<>(apiResult.getStatus(), apiResult.getMsg(), null, 0, 0, 0);
        ListResult<List<ReportResult>> result = new ListResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            int adminId = 0;
            Admin admin = adminMapper.selectById(userId);
            if (admin.getType() == 1) {
                adminId = admin.getId();
            }
            Page<Report> pages = new Page<>(page, limit);
            IPage<Report> iPages = baseMapper.selectBySearch(pages, tel, phone, reportStatus, adminId);
            List<ReportResult> reportResults = new ArrayList<>();
            for (Report report : iPages.getRecords()) {
                ReportResult reportResult = new ReportResult();
                reportResult.setId(report.getId());
                reportResult.setAdminId(report.getAdminId());
                reportResult.setCreateAdmin(report.getCreateAdmin());
                reportResult.setCreateAdminName(report.getCreateAdminName());
                reportResult.setAdminName(report.getAdminName());
                reportResult.setContent(report.getContent());
                reportResult.setContentFullPath(website + report.getContent());
                reportResult.setStatus(report.getStatus());
                reportResult.setCreateTime(Global.longToString(report.getCreateTime()));
                reportResults.add(reportResult);
            }
            result.setData(reportResults);
            result.setPage(page);
            result.setLimit(limit);
            result.setTotal(iPages.getTotal());
            msg = "获取成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> addReport(Integer userId, String token, Integer adminId, String content) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, adminId, content), userId, token, "1202");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "创建失败";
        try {
            Admin admin = adminMapper.selectById(adminId);
            if (admin != null) {
                Report report = new Report();
                InputStream inputStream = org.apache.commons.io.IOUtils.toInputStream(content, "UTF-8");
                String path = FileHandleUtil.upload(inputStream,
                        System.currentTimeMillis()
                                + "_"
                                + userId
                                + "."
                                + "html", configPath, "html",true);
                report.setContent(path);
                report.setAdminId(adminId);
                report.setCreateAdmin(userId);
                report.setCreateTime(System.currentTimeMillis());
                baseMapper.insert(report);
                status = 1;
                msg = "创建成功";
            } else msg = "举报用户不存在";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> editReport(Integer userId, String token, Integer ids, String content) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, ids, content), userId, token, "1203");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "更新失败";
        try {
            Report report = baseMapper.selectById(ids);
            if (report != null) {
                report.setContent(content);
                baseMapper.updateById(report);
                status = 1;
                msg = "更新成功";
            } else msg = "举报不存在";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> deleteReport(Integer userId, String token, Integer[] ids) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "1204");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "删除失败";
        try {
            for (Integer id : ids) {
                Report report = baseMapper.selectById(id);
                if (report == null) {
                    continue;
                }
                baseMapper.deleteById(id);
            }
            msg = "删除成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> auditReport(Integer userId, String token, Integer ids, Integer reportStatus) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, ids), userId, token, "1203");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "更新失败";
        try {
            Report report = baseMapper.selectById(ids);
            if (report != null) {
                Admin admin = adminMapper.selectById(report.getAdminId());
                if (admin != null) {
                    report.setStatus(reportStatus);
                    baseMapper.updateById(report);

                    if (reportStatus == 2) {
                        admin.setBlacklist(1);
                        adminMapper.updateById(admin);
                    }
                    status = 1;
                    msg = "更新成功";
                } else msg = "举报用户不存在";
            } else msg = "举报不存在";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
}
